<template>
  <div class="mod-demo__user"style="overflow-y: scroll;height: 800px;" >
      <el-descriptions>
        <el-descriptions-item label="项目名称">{{routeParams.projectName}}</el-descriptions-item>
      </el-descriptions>
   <div style="height: 10px"></div>

    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item prop="projectId">
        <el-input style="display: none" v-model="state.dataForm.projectId" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item label="用户呢称">
        <el-input v-model="state.dataForm.username" placeholder="请输入用户呢称" clearable></el-input>
      </el-form-item >
      <el-form-item label="用户账号">
        <el-input v-model="state.dataForm.mobile" placeholder="请输入用户账号" clearable></el-input>
      </el-form-item>

      <el-form-item  label="项目身份">
        <el-select v-model="state.dataForm.userrole"  placeholder="请选择项目身份 ">
          <el-option
            v-for="item in state.userroleOp"
            :key="item"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="state.getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateHandle(state.dataForm.projectId)">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border
              @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
      <!--              <el-table-column prop="id" label="id" header-align="center" align="center"></el-table-column>-->
      <el-table-column prop="username" label="用户名呢称" header-align="center" align="center"></el-table-column>
      <el-table-column prop="mobile" label="用户账号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="userrole" label="项目身份" sortable="custom" header-align="center" align="center">
        <template v-slot="scope">
          <el-tag v-if="scope.row.userrole === 1" size="small" type="danger">施工人员</el-tag>
          <el-tag v-if="scope.row.userrole === 2" size="small" type="info">施工负责人</el-tag>
          <el-tag v-if="scope.row.userrole === 3" size="small" type="success">管理人员</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createDate" label="加入时间" header-align="center" align="center"></el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button type="primary" link @click="updateProUpdateHandle(scope.row)">修改</el-button>
          <el-button type="primary" link @click="state.deleteHandle(scope.row.userProjectSubId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
                   :total="state.total" layout="total, sizes, prev, pager, next, jumper"
                   @size-change="state.pageSizeChangeHandle"
                   @current-change="state.pageCurrentChangeHandle"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <AddOrUpdate ref="addOrUpdateRef" @refreshDataList="state.getDataList">确定</AddOrUpdate>
    <updatePro ref="updateRef" @refreshDataList="state.getDataList">确定</updatePro>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import {reactive, ref, toRefs,watch} from "vue";
import {useRoute,useRouter} from "vue-router";
import AddOrUpdate from "./userPro-add.vue";
import updatePro from "./userPro-update.vue";
import {IObject} from "@/types/interface";


const view = reactive({
  deleteIsBatch: true,
  getDataListURL: "/sysapp/user/pageByPro",
  getDataListIsPage: true,
  exportURL: "/sysapp/user/export",
  deleteURL: "/sysapp/user/deleteUserProjectSub",
  dataForm:{
    projectId:"",
  },
  userroleOp:[{label:"施工人员", value:1},{label:"施工负责人" ,value:2}]
});

const route = useRoute();
const routeParams = ref({});
routeParams.value = route.params;

const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addOrUpdateRef.value.init(id);
};
const updateRef = ref();
const updateProUpdateHandle = (row?:IObject) => {
  updateRef.value.init(row)
}
const state = reactive({...useView(view), ...toRefs(view)});
 //开始获取路由带过来的参数

console.log("开始初始化参数")
//开始给state的param赋值
if(state.dataForm.projectId == ""){
  if(null !=routeParams.value.projectId){
    console.log("获取参数不为空")
    state.dataForm.projectId = routeParams.value.projectId;
    state.getDataList();
  }else{
    console.log("获取参数为空")
  }
}
watch(() => route.params?route.params:undefined, (newId?:{}) => {
  console.log("开始监视路由变化")
  console.log()
  if(Object.keys(newId).length !== 0){
    if(routeParams.value.projectId != newId.projectId){
      routeParams.value = newId;
      state.dataForm.projectId = routeParams.value.projectId;
      state.getDataList();
    }
  }})
</script>
